package com.asset.dispoal.business.bsAgreement.service.commom;

import com.asset.dispoal.business.bsAgreement.domain.db.entity.AgreementStateRecord;
import com.asset.dispoal.business.bsAgreement.domain.db.entity.SingleAgreementCaseState;
import com.asset.dispoal.business.bsAgreement.domain.db.mapper.AgreementMapper;
import com.asset.dispoal.business.bsAgreement.dto.AgreementDataResponse;
import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Component
public class AgreementResumeCommand implements BaseCommand<String, Result> {

    @Autowired
    private AgreementMapper agreementMapper;

    @Autowired
    private BsCaseMapper caseMapper;

    @Autowired
    private BaseIdeable<String> ideable;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result execute(String id) {
        SingleAgreementCaseState agreementCaseState = agreementMapper.querySingleAgreementCaseStateByAgreementId(id);
        String caseId = agreementCaseState.getCaseId();
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        if (StringUtil.isNotEmpty(caseId)) {
            //修改案件并且添加批次
            BsCase cases = new BsCase();
            cases.setId(caseId);
            caseMapper.insertMapping(id, cases.getId());
            cases.setOperateUser(userId);
            cases.setOperateTime(DateTimeUtil.currentDate());
            cases.setLawsuitPrincipalAmount(nullToZero(agreementCaseState.getLawsuitPrincipalAmount()).add(agreementCaseState.getContractLawsuitPrincipalAmount()));
            cases.setCreditPrincipalAmount(nullToZero(agreementCaseState.getCreditPrincipalAmount()).add(agreementCaseState.getContractCreditPrincipalAmount()));
            cases.setLawsuitInterestAmount(nullToZero(agreementCaseState.getLawsuitInterestAmount()).add(agreementCaseState.getContractLawsuitInterestAmount()));
            caseMapper.updateAmount(cases);
        } else {
            //添加案件并且添加批次
            AgreementDataResponse currentData = agreementMapper.queryAgreementData(id);
            BsCase cases = new BsCase();
            cases.setId(ideable.generateId());
            caseMapper.insertMapping(id, cases.getId());
            cases.setBorrowerAddress(currentData.getBorrowerAddress());
            cases.setBorrowerIdcard(currentData.getBorrowerIdcard());
            cases.setBorrowerTel(currentData.getBorrowerTel());
            cases.setAssetInstBatchId(currentData.getAssetInstBatchId());
            cases.setBorrowerName(currentData.getBorrowerName());
            cases.setBorrowerNation(currentData.getBorrowerNation());
            cases.setLawsuitPrincipalAmount(agreementCaseState.getContractLawsuitPrincipalAmount());
            cases.setLawsuitInterestAmount(agreementCaseState.getLawsuitInterestAmount());
            cases.setCreditPrincipalAmount(agreementCaseState.getCreditPrincipalAmount());
            cases.setCreateUser(userId);
            cases.setCreateTime(DateTimeUtil.fromDateTime(LocalDateTime.now()));
            if (AgreementCaseSateMenu.CLOSE_CASE.getLabel().equals(agreementCaseState.getLastState())) {
                cases.setCaseState(AgreementCaseSateMenu.CLOSE_CASE.getLabel());
            } else {
                cases.setCaseState(AgreementCaseSateMenu.GATHER.getLabel());
            }
            caseMapper.insert(cases);

        }
        String oldState = agreementMapper.getOldStateByContractId(id);
        AgreementCaseSateMenu state = AgreementCaseSateMenu.getAddLabel(oldState);
        agreementMapper.updateCaseState(id, state.getLabel(), state.getState(), userId);
        AgreementStateRecord agreementStateRecord = new AgreementStateRecord();
        agreementStateRecord.setId(ideable.generateId());
        agreementStateRecord.setContractId(id);
        agreementStateRecord.setCreateUser(userId);
        agreementStateRecord.setCreateTime(DateTimeUtil.currentDate());
        agreementStateRecord.setOldState(agreementCaseState.getCaseState());
        agreementStateRecord.setNewState(oldState);
        agreementMapper.insertStateRecord(agreementStateRecord);
        return Result.ok();
    }

    /**
     * null值处理为 0 值
     *
     * @param bigDecimal
     * @return
     */
    private BigDecimal nullToZero(BigDecimal bigDecimal) {
        return bigDecimal == null ? BigDecimal.ZERO : bigDecimal;
    }
}
